package appcus

import (
	"counter-help/admin/server/model/common/response"
	"github.com/gin-gonic/gin"
)

// UserSettingsApi 用户设置控制器
type UserSettingsApi struct{}

// CreateUserSettings 创建用户设置
func (a *UserSettingsApi) CreateUserSettings(c *gin.Context) {
	userID, _ := c.Get("userID") // 获取当前登录用户的ID

	if err := userSettingsService.CreateUserSettings(userID.(uint)); err != nil {
		response.FailWithMessage("创建用户设置失败", c)
		return
	}
	response.OkWithMessage("用户设置创建成功", c)
}

// UpdateUserSettings 更新用户设置
func (a *UserSettingsApi) UpdateUserSettings(c *gin.Context) {
	var settings struct {
		AcceptOtherMsg  bool `json:"acceptOtherMsg"`
		AcceptSysMsg    bool `json:"acceptSysMsg"`
		AcceptPublicMsg bool `json:"acceptPublicMsg"`
	}

	userID, _ := c.Get("userID") // 获取当前登录用户的ID

	// 绑定请求体中的数据
	if err := c.ShouldBindJSON(&settings); err != nil {
		response.FailWithMessage("参数解析失败", c)
		return
	}

	if err := userSettingsService.UpdateUserSettings(userID.(uint), settings.AcceptOtherMsg, settings.AcceptSysMsg, settings.AcceptPublicMsg); err != nil {
		response.FailWithMessage("更新用户设置失败", c)
		return
	}
	response.OkWithMessage("用户设置更新成功", c)
}

// GetUserSettings 获取用户设置
func (a *UserSettingsApi) GetUserSettings(c *gin.Context) {
	userID, _ := c.Get("userID") // 获取当前登录用户的ID

	settings, err := userSettingsService.GetUserSettings(userID.(uint))
	if err != nil {
		response.FailWithMessage("获取用户设置失败", c)
		return
	}
	response.OkWithData(gin.H{"settings": settings}, c)
}
